package com.hertz.alarm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hertz.alarm.controller.support.ResultModel;
import com.hertz.alarm.entity.IrisTrigger;
import com.hertz.alarm.entity.Team;
import com.hertz.alarm.service.EntityService;
import com.hertz.alarm.service.SystemTriggers;
import com.hertz.alarm.util.Constants;
import com.hertz.alarm.util.DateUtils;
import com.hertz.alarm.util.ReturnCodeConstants;

@Controller
@RequestMapping("/admin/task")
public class TaskController extends BaseController {
	@Resource
	private SystemTriggers systemTriggers;
	@Resource
	private EntityService entityService;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	public ModelAndView getTeamNba(HttpServletRequest request){
		List<IrisTrigger> list = systemTriggers.listALl();
		Map<String, Object> dataMap = new HashMap<String,Object>();
		dataMap.put("systemTriggers", list);
		return returnModelAndView(dataMap, request);
	}
	@RequestMapping(value="runOrStop",method=RequestMethod.GET)
	public @ResponseBody ResultModel<Object> runOrStop(
			HttpServletRequest request,
			@RequestParam(value = "id", required = true) short id,
			@RequestParam(value = "action", required = true) String action
			){
		IrisTrigger it = entityService.getById(id, IrisTrigger.class);
		if(action.equals("start")){
			it.setFlag(true);
		}
		if(action.equals("stop")){
			it.setFlag(false);
		}
		systemTriggers.update(it);
		if(it.getFlag()){
			new MyThread().start();
		}
		return buildResultModel(null, ReturnCodeConstants.SUCCESS);
	}
	
	public class MyThread extends Thread {
		
		public void run() {
			String ymd = DateUtils.today(DateUtils.Y_M_D);
			systemTriggers.fetchMatchData(ymd);
		}
			
	}
}
